//
//  UITextViewExtensions.swift
//  SwiftExtensionDemo
//
//  Created by 腾億 on 2017/11/30.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

extension UITextView {
    #if os(iOS)
    
    //自动将带有完成按钮的工具栏添加到键盘的顶部。 点击按钮将关闭键盘。
    public func addDoneButton(_ barStyle: UIBarStyle = .default, title: String? = nil) {
        let keyboardToolbar = UIToolbar()
        keyboardToolbar.items = [
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
            UIBarButtonItem(title: title ?? "Done", style: .done, target: self, action: #selector(resignFirstResponder))
        ]
        
        keyboardToolbar.barStyle = barStyle
        keyboardToolbar.sizeToFit()
        
        inputAccessoryView = keyboardToolbar
    }
    
    #endif
}
